// pages/settings/settings.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeOptions: ['1', '2', '3', '4', '5'],
    timeIndex: 2, // 默认3分钟
    difficultyOptions: ['简单', '中等', '困难'],
    difficultyIndex: 0 // 默认简单
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 加载保存的设置
    const gameTime = wx.getStorageSync('gameTime') || 3;
    const difficulty = wx.getStorageSync('difficulty') || '简单';
    
    this.setData({
      timeIndex: this.data.timeOptions.indexOf(gameTime.toString()),
      difficultyIndex: this.data.difficultyOptions.indexOf(difficulty)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onTimeChange(e) {
    this.setData({
      timeIndex: e.detail.value
    });
  },

  onDifficultyChange(e) {
    this.setData({
      difficultyIndex: e.detail.value
    });
  },

  saveSettings() {
    const gameTime = this.data.timeOptions[this.data.timeIndex];
    const difficulty = this.data.difficultyOptions[this.data.difficultyIndex];
    
    wx.setStorageSync('gameTime', parseInt(gameTime));
    wx.setStorageSync('difficulty', difficulty);
    
    wx.showToast({
      title: '设置已保存',
      icon: 'success',
      duration: 2000
    });
    
    setTimeout(() => {
      wx.navigateBack();
    }, 2000);
  }
})